<?php
declare(strict_types=1);

namespace App\Controller;

use App\Common\Response\SystemCode;
use App\Job\WxReplaceMediaJob;
use App\Model\Applet;
use App\Model\AuthRole;
use App\Model\Tag;
use App\Model\Company;
use App\Model\DataBackhaulSetting;
use App\Model\DataSpread;
use App\Model\DataSpreadPlatform;
use App\Model\Module;
use App\Model\Playlet;
use App\Model\RechargeTemplate;
use App\Service\CommicService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Utils\ApplicationContext;
class BaseController
{

    public static $commonStatus = [
        '0'       => '禁用',
        '1'       => '正常',
        '2'       => '未启用',
        '3'       => '重置密码',
    ];

    public function response($code, $msg = '', $data = '')
    {
        if (is_array($msg)) {
            $msg = implode(' ', array_column($msg, 0));
        }

        return [
            'code'    => $code,
            'data'    => $data,
            'message' => $msg,
        ];
    }


    public function templateData(){

        $data =[
            'name' => '',
            'status' => 1,
            'convention' =>[
                ['name' => '常规档位','amount' => '0','give_coin' => 0,'status' => true,'type' => 1,'day' => 0],
                ['name' => '常规档位','amount' => '0','give_coin' => 0,'status' => true,'type' => 1,'day' => 0],
                ['name' => '常规档位','amount' => '0','give_coin' => 0,'status' => true,'type' => 1,'day' => 0],
                ['name' => '常规档位','amount' => '0','give_coin' => 0,'status' => true,'type' => 1,'day' => 0],
            ],
            'member' => [
                ['name' => '1天会员','amount' => '0','give_coin' => 0,'status' => false,'type' => 3,'day' => 1],
                ['name' => '3天会员','amount' => '0','give_coin' => 0,'status' => false,'type' => 3,'day' => 3],
                ['name' => '7天会员','amount' => '0','give_coin' => 0,'status' => false,'type' => 3,'day' => 7],
                ['name' => '15天会员','amount' => '0','give_coin' => 0,'status' => false,'type' => 3,'day' => 15],
                ['name' => '月度会员','amount' => '0','give_coin' => 0,'status' => false,'type' => 3,'day' => 31],
                ['name' => '季度会员','amount' => '0','give_coin' => 0,'status' => false,'type' => 3,'day' => 90],
                ['name' => '半年会员','amount' => '0','give_coin' => 0,'status' => false,'type' => 3,'day' => 180],
                ['name' => '年度会员','amount' => '0','give_coin' => 0,'status' => false,'type' => 3,'day' => 366],
            ],
            'promotion' => [
                ['name' => '整剧购买','amount' => '0','give_coin' => 0,'status' => false,'type' => 2,'day' => 0],
            ],
        ];


        return $data;
    }

    public function get_code($code_code=''){
        $code = [
            '10000' => '成功',
            '20001' => 'open_id不能为空！',
            '20002' => '授权失败！',
            '20030' => '当前章节没有购买的！',
            '20040' => '任务ID不能外空！',
            '20041' => '任务不存在',
            '20042' => '当前任务已经领取',
            '20043' => '不满足条件',
            '20050' => '商品ID不能为空！',
            '20060' => '缺少必要参数！',
            '20061' => '余额不足！',
        ];
        return isset($code[$code_code])? $code[$code_code]:'';
    }

    public function get_applet_id(){
        # 获取肚下程序applet_id
        $request = ApplicationContext::getContainer()->get(RequestInterface::class);
        $headers = $request->getHeaders();
//        echo "headers".print_r($headers).PHP_EOL;
        return $headers["applet_id"][0];
    }

    public function get_ip(){
        $request = ApplicationContext::getContainer()->get(RequestInterface::class);
        $headers = $request->getHeaders();
        if(isset($headers['x-forwarded-for'][0]) && !empty($headers['x-forwarded-for'][0])) {
            return $headers['x-forwarded-for'][0];
        } elseif (isset($headers['x-real-ip'][0]) && !empty($headers['x-real-ip'][0])) {
            return $headers['x-real-ip'][0];
        }elseif(isset($headers['remoteip'][0]) && !empty($headers['remoteip'][0])) {
            return $headers['remoteip'][0];
        }
        $serverParams = $request->getServerParams();
        return $serverParams['remote_addr'] ?? '';
    }

    public function get_user_ua(){
        $request = ApplicationContext::getContainer()->get(RequestInterface::class);
        $headers = $request->getHeaders();
        return isset($headers["user-agent"][0]) ? $headers["user-agent"][0] : "";
    }

    public function company(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',Company::select(['id as value','name as label'])->where(['status' => Company::STATUS_SHOW])->get());
    }
    public function module(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',Module::select(['id as value','name','label'])->get());
    }
    public function AuthRole(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',AuthRole::select(['id as value','name as label'])->where(['status' => AuthRole::STATUS_SHOW])->get());
    }
    public function applet(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',Applet::select(['id as value','name as label'])->where(['status' => Applet::STATUS_SHOW])->get());
    }

    public function playlet(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',Playlet::select(['id as value','name as label'])->where(['status' => Playlet::STATUS_SHOW])->get());
    }

    public function templete(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',$this->templateData());
    }

    public function platform(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',DataSpreadPlatform::select(['id as value','name as label'])->get());
    }

    public function backhaulSetting(int $id){


        //查询对应的推广链接
        $dataSpreadInfo  = DataSpread::query()->where(['id' => $id])->first();
        if(empty($dataSpreadInfo)){

            return $this->response(SystemCode::SYSTEM_ERROR, '推广链接不存在');
        }

        $resultData[] = ['value' => 0,'label' => '不回传'];

        $returnData = DataBackhaulSetting::select(['id as value','name as label'])
            ->where(['company_id' =>$dataSpreadInfo->company_id,'applet_id' => $dataSpreadInfo->applet_id])
            ->get()->toArray();

        if(!empty($returnData)){

            $resultData = array_merge($resultData,$returnData);
        }

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',$resultData);
    }

    public function templeteList(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',RechargeTemplate::select(['id as value','name as label'])->where(['status' => RechargeTemplate::STATUS_SHOW])->get());
    }

    public function Tag(){

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',Tag::select(['id as value','name as label'])->get());
    }

    public function templateType(){

        $data = [
            ['label' => '小剧场','value' => 1],
            ['label' => '剧情介绍','value' => 2],
            ['label' => '故事投稿','value' => 3],
            ['label' => '剧情墙纸','value' => 4]
        ];

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',$data);
    }


    public function jumpType(){

        $data = [
            ['label' => '我的','value' => 1],
            ['label' => '任务','value' => 2],
            ['label' => '排行','value' => 3],
            ['label' => '追剧','value' => 4],
            ['label' => '添加收藏','value' => 5],
            ['label' => '充值','value' => 6],
            ['label' => '充值记录','value' => 7],
            ['label' => '点赞历史','value' => 8],
            ['label' => '联系客服','value' => 9],
            ['label' => '推荐','value' => 10],
        ];

        return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',$data);
    }

}